package com.lz.sports.adapter

import android.graphics.Paint
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ObservableBoolean
import com.lz.library.adapter.BindingAdapter
import com.lz.library.adapter.BindingViewHolder
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.bean.ExamResult
import com.lz.sports.databinding.ItemExamMBinding
import com.lz.sports.widget.ShakeClickListener

//学生管理
class ExamManagerAdapter : BindingAdapter<ExamResult>() {

    private var showAllInfo = ObservableBoolean(false)

    override fun getLayoutResId(viewType: Int): Int = R.layout.item_exam_m

    override fun getVariableId(viewType: Int): Int = BR.result

    override fun onCreateItem(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val vh = super.onCreateItem(parent, viewType)
        val binding = vh.getBinding<ItemExamMBinding>()
        binding.tvId.paint.apply { flags = Paint.UNDERLINE_TEXT_FLAG; isAntiAlias = true }
        binding.tvId.setOnClickListener(object : ShakeClickListener() {
            override fun shakeClick(v: View) {
                val position = vh.bindingAdapterPosition
                if (position < 0) return
                onIdCardClickListener?.onIdCardClick(position)
            }
        })
        return vh
    }

    override fun onBindViewHolder(vh: BindingViewHolder, position: Int) {
        super.onBindViewHolder(vh, position)
        val binding = vh.getBinding<ItemExamMBinding>()
        binding.showAll = showAllInfo
    }

    fun changeShowState(show: Boolean) {
        if (showAllInfo.get() == show) return
        this.showAllInfo.set(show)
    }

    fun selectAll() {
        data.forEach { it.changeSelected() }
    }

    fun selectList(): List<ExamResult> = data.filter { it.isSelected.get() && !it.isLack() }

    var onIdCardClickListener: OnIdCardClickListener? = null

    interface OnIdCardClickListener {

        fun onIdCardClick(position: Int)
    }
}